package com.tensquare.base.rest;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.sound.sampled.Port;

@RestController
@RequestMapping("label")
@CrossOrigin
public class BaseRest {

    @Autowired
    private LabelService labelService;
    @Value("${server.port}")
    int port;


    /**
     * http://localhost:9001/list/1/10
     * @param page
     * @return
     */
    @RequestMapping(value = "/list/{page}/{limit}", method = RequestMethod.GET)
    public Result list(@PathVariable("page") int page, @PathVariable("limit") int limit){

        return new Result(true, StatusCode.OK, "查询成功", labelService.findAll(page, limit));
    }


    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable("id") String id){
        System.err.println("调用了微服务的模块接口, 端口："  + port);
        return new Result(true, StatusCode.OK, "查询成功" + port, labelService.findById(id));
    }

    /**
     * url://http://localhost:9001/label/add?id=002&labelname=测试2&state-2&count=2&fans=2&recommend=2
     * @param label
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Label label){
        labelService.add(label);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result update(@RequestBody Label label){
        labelService.update(label);
        return new Result(true, StatusCode.OK, "更新成功" );
    }

        @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") String id){
        labelService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
